﻿#if NET451_OR_GREATER
using System.Web.Http;
using System.Linq;
using System.Net;
using System;
using System.Collections.Generic;
#endif

#if NET6_0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
#endif

namespace T.SF.Console
{
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }

#if NET451_OR_GREATER
    public class DemoController : ApiController
    {
        Product[] products = new Product[]
        {
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
        };

        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }

        public Product GetProductById(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return product;
        }

        public IEnumerable<Product> GetProductsByCategory(string category)
        {
            return products.Where(p => string.Equals(p.Category, category,
                    StringComparison.OrdinalIgnoreCase));
        }
    }
#endif

#if NET6_0
    [Route("api/demo")]
        [ApiController]
        public class DemoController : ControllerBase
        {
            Product[] products = new Product[]
            {
                    new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
                    new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
                    new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
            };

            // GET api/values
            [HttpGet]
            public ActionResult<IEnumerable<Product>> Get()
            {
                return products;
            }

            // GET api/values/5
            [HttpGet("{id}")]
            public ActionResult<Product> Get(int id)
            {
                var product = products.FirstOrDefault((p) => p.Id == id);
                if (product == null)
                {
                    throw new WebException("Data not exist!", WebExceptionStatus.UnknownError);
                }
                return product;
            }

            // POST api/values
            [HttpPost]
            public void Post([FromBody] string value)
            {
            }

            // PUT api/values/5
            [HttpPut("{id}")]
            public void Put(int id, [FromBody] string value)
            {
            }

            // DELETE api/values/5
            [HttpDelete("{id}")]
            public void Delete(int id)
            {
            }
        }
#endif
}
